package com.example.redissiondemo;

import org.junit.jupiter.api.Test;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.util.AssertionErrors;
import org.springframework.util.Assert;

import java.util.concurrent.TimeUnit;

@SpringBootTest
class RedissionDemoApplicationTests2 {

    @Autowired
    private RedissonClient redissonClient;

    @Test
    void contextLoads() throws InterruptedException {
        lock    ();
    }

    private void lock() throws InterruptedException {
        //获取锁对象实例
        final String lockKey = "lock";
        RLock rLock = redissonClient.getLock(lockKey);

        boolean b = rLock.tryLock(2, TimeUnit.SECONDS);
        AssertionErrors.assertTrue("拿不到锁", b);

        try {
            System.out.println("获取锁成功" + Thread.currentThread().getId());
            Thread.sleep(23000);
        } finally {
            //无论如何, 最后都要解锁
            rLock.unlock();
        }
    }

}
